﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using blackhouse.BlackApart.Data;

namespace blackhouse.BlackApart.Business.Prices {
    public class DaApartmentPriceProfile : IApartmentPrice {

        #region Fields

        private int apartmentId;
        private IPricesDataAccess dataAccess;
        private ImpApartmentPriceProfile instance;

        #endregion

        #region Properties

        public int ReservationNumberOfPeoples {
            get { return this.instance.ReservationNumberOfPeoples; }
            set { this.instance.ReservationNumberOfPeoples = value; }
        }

        public DateTime ReservationFromDay {
            get { return this.instance.ReservationFromDay; }
            set { this.instance.ReservationFromDay = value; }
        }

        public DateTime ReservationToDay {
            get { return this.instance.ReservationToDay; }
            set { this.instance.ReservationToDay = value; }
        }

        #endregion

        #region Constructors

        public DaApartmentPriceProfile(int apartmentId, IPricesDataAccess iPricesDataAccess) {
            this.apartmentId = apartmentId;
            this.dataAccess = iPricesDataAccess;
            this.instance = new ImpApartmentPriceProfile();
            this.SetInstanceVariants();
            this.SetInstanceModification();
        }

        #endregion

        #region Methods

        private void SetInstanceVariants() {
            VariantLineData[] data = this.dataAccess.GetPriceProfileVariants(this.apartmentId);
            Dictionary<int, PriceProfileVariant> variants = new Dictionary<int, PriceProfileVariant>();
            foreach (VariantLineData line in data) {
                if (!variants.ContainsKey(line.VariantId)) {
                    PriceProfileVariant variant = new PriceProfileVariant(line.FromDate, line.ToDate);
                    variant.DefaultPrice = line.DefaultPrice;
                    variant.DisableRestModifications = line.DisableRestModifications;
                    variants.Add(line.VariantId, variant);
                }
                variants[line.VariantId].AddItem(line.PeopleCount, line.Price);
            }
            foreach (PriceProfileVariant variant in variants.Values) {
                this.instance.AddVariant(variant);
            }
        }

        private void SetInstanceModification() {
            this.SetInstanceDateModification();
            this.SetInstanceLengthOfStayModification();
        }

        private void SetInstanceDateModification() {
            DateModificationData[] data = this.dataAccess.GetPriceProfileDateModification(this.apartmentId);
            foreach (DateModificationData line in data) {
                PriceProfileModification mod = new PriceProfileDateModification(line.RangeToStartReservationFrom, line.RangeToStartReservationTo, (PriceProfileModificationType) Enum.Parse(typeof(PriceProfileModificationType), line.ModificationType), line.ModificationValue);
                this.instance.AddModification(mod);
            }
        }

        private void SetInstanceLengthOfStayModification() {
            LengthOfStayModificationData[] data = this.dataAccess.GetPriceProfileLengthOfStayModification(this.apartmentId);
            foreach (LengthOfStayModificationData line in data) {
                PriceProfileModification mod = new PriceProfileLengthOfStayModification(line.LengthOfStayGreaterOrEqual, line.LengthOfStayLess, (PriceProfileModificationType) Enum.Parse(typeof(PriceProfileModificationType), line.ModificationType), line.ModificationValue);
                this.instance.AddModification(mod);
            }
        }

        public double GetPrice() {
            return this.instance.GetPrice();
        }

        public double GetPriceWithoutRabat() {
            return this.instance.GetPriceWithoutRabat();
        }

        public float GetRabat()
        {
            return this.instance.GetRabat();
        }

        #endregion

    }
}
